package com.ebai.product.management.controller.system;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ebai.product.management.base.cache.service.PublicCacheImpl;
import com.ebai.product.management.base.controller.WebController;
import com.ebai.product.management.base.utils.Common;
import com.ebai.product.management.framework.aspectj.lang.annotation.Log;
import com.ebai.product.management.model.entity.User;
import com.ebai.product.management.service.system.RoleService;
import com.ebai.product.management.service.system.UserService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/system/user")
public class UserController extends WebController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private UserService userService;
    
    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    @RequiresPermissions("system:user:view")
    public String indexPage(ModelMap modelMap) {
    	modelMap.addAttribute("enableTypeList", PublicCacheImpl.getDictListByCode("TYSF"));
        return "system/user/index";
    }

    /**
     * AJAX调用分页数据信息
     * @param response HTTP响应
     * @param page 当前页码
     * @param limit 分页条数
     * @param sort 排序方式
     * @param order 排序字段
     * @param condition 查询条件
     */
    @RequestMapping("/pageList")
    public void jsonPageList(HttpSession session, HttpServletResponse response, int page, int limit, String sort, String order, User condition) {
        JSONObject json = Common.getJsonDatagrid(this.userService.queryPage(page, limit, condition));
        if (logger.isDebugEnabled()) {
            logger.debug(json.toString());
        }
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }
    
    @RequestMapping("/pageListByRole")
    public void jsonPageListByRole(HttpSession session, HttpServletResponse response, int page, int limit, String sort, String order, String rolePid) {
        JSONObject json = Common.getJsonDatagrid(this.userService.queryPageByRole(page, limit, rolePid));
        if (logger.isDebugEnabled()) {
            logger.debug(json.toString());
        }
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }

    /**
     * 新增或修改时的表单页面
     * @param modelMap
     * @return
     */
    @Log(title = "系统管理", action = "用户管理-编辑用户")
    @RequestMapping("/form")
    public String formPage(ModelMap modelMap) {
        return "system/user/form";
    }

    /**
     * 检查登录账号是否重否
     * @param response
     * @param loginName
     */
    @RequestMapping("/checkExist")
    public void jsonDataExist(HttpServletResponse response, String loginName) {
        JSONObject result = Common.getJsonResult(this.userService.checkExist(loginName));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 表单数据保存
     * @param response
     * @param data
     */
    @RequestMapping("/save")
    public void jsonDataSave(HttpServletResponse response, User data) {
        JSONObject result = Common.getJsonResult(this.userService.saveData(data));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 列表数据删除
     * @param response
     * @param ids
     */
    @RequestMapping("/drop")
    public void jsonDataDrop(HttpServletResponse response, String ids) {
        JSONObject result = Common.getJsonResult(this.userService.dropData(ids));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }

    /**
     * 用户密码重置
     * @param response
     * @param ids
     */
    @RequestMapping("/undoPasswd")
    public void jsonDataUndoPasswd(HttpServletResponse response, String ids) {
        JSONObject result = Common.getJsonResult(this.userService.undoPasswd(ids));
        this.sendDirectToClient(response, CONTENTTYPE_JSON, result.toString());
    }
    
    @RequestMapping("/setUserRoles")
    public String setUserRoles(ModelMap modelMap, String userid) {
    	modelMap.addAttribute("roleList", this.roleService.getRoleCheckData(userid));
        return "system/user/setUserRoles";
    }
}
